function add_new_address() {
	nextView("addNewAddress.html");
}

function select(selectIndex) {
	var addresses = $('body,.store_address_select');
	for (var i = 0; i <= addresses.length; i++) {
		var item = addresses.children().eq(i);
		if(selectIndex == i){
			item.find('.store_address_select').show();
			item.removeClass('address_manage_item');
			item.addClass('address_manage_item_select');
		}else{
			item.find('.store_address_select').hide();
			item.removeClass('address_manage_item_select');
			item.addClass('address_manage_item');
		}
	}
	dataSave("addressIndex", selectIndex);
	backPage();
}

$(function(){
	var userUuid = dataGet("userUuid");
	var data= "&crarsUser=" + userUuid;
    var url = urlfile + "coreAddress/find/my/address";

    ajaxTool("post",data,url,
        function error(XMLHttpRequest, textStatus, errorThrown,fnErr){
            alert("error:" + XMLHttpRequest);
        },
        function success(data){
            if(!data.success) {
                alert(data.errMsg);
            }else{
            	if(data.data && data.data.length>0){
            		var addressHtml="";
            		for(var i=0; i<data.data.length; i++){
            			var isSelsect="";
      					var select = dataGet("addressIndex");
      					var addressCheck = "";
	            		if(select==null || select>data.data.length){
	            			select=0;
	            		}
		
            			if(i==select) {
            				isSelsect = '<div class="address_manage_item_select" onclick="select('+i+')">';
            			}else{
            				isSelsect = '<div class="address_manage_item" onclick="select('+i+')">';
            			}

            			if(data.data[i].crarsDefault == 1) {
            				addressCheck = '<div class="store_address_select">默认</div>';
            			}
            			addressHtml+= 
            			isSelsect+
							'<div class="address_top">'+
								'<div class="address_top_name">'+data.data[i].crarsName+'</div>'+
								'<div class="address_top_phoneNo">'+data.data[i].crarsTel+'</div>'+
							'</div>'+


							'<div class="address_operation">'+
								addressCheck+
								'<div class="address_edit btn" onclick="editAddress(\''+ data.data[i].crarsUuid +'\')"></div>'+
								'<div class="address_delete" onclick="deleteAddress(\''+ data.data[i].crarsUuid +'\')"></div>'+
							'</div>'+

							'<div class="address_bottom">'+
								'<div style="float:left;margin-left: 10px;">收货地址：'+data.data[i].crarsCity +data.data[i].crarsAddress+'</div>'+
							'</div>'+
							
						'</div>';
            		}
            		$('body').prepend(addressHtml);
            	}
            }
        }
    );

})

function editAddress(Uuid) {
	nextView("editAddress.html?Uuid=" + Uuid);
	stopDeliverEvent();
}

function deleteAddress(Uuid) {
	var data= "&crarsUuid=" + Uuid;
    var url = urlfile + "coreAddress/delete/one";
    ajaxTool("post",data,url,
        function error(XMLHttpRequest, textStatus, errorThrown,fnErr){
            alert("error:" + XMLHttpRequest);
        },
        function success(data){
            if(!data.success) {
                alert(data.errMsg);
            }else{
            	refreshPage();
            }
        }
    );

	stopDeliverEvent();
}